package com.travel.travel_springboot_web.mapper;

import com.travel.travel_springboot_web.entity.Address;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AddressMapper {
    @Insert("INSERT INTO address (user_id, name, phone, province, city, district, detail, " +
            "is_default, created_at, updated_at) " +
            "VALUES (#{userId}, #{name}, #{phone}, #{province}, #{city}, #{district}, " +
            "#{detail}, #{isDefault}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Address address);

    @Select("SELECT * FROM address WHERE user_id = #{userId} ORDER BY is_default DESC, created_at DESC")
    List<Address> findByUserId(Long userId);

    @Update("UPDATE address SET name = #{name}, phone = #{phone}, " +
            "province = #{province}, city = #{city}, district = #{district}, " +
            "detail = #{detail}, is_default = #{isDefault}, updated_at = NOW() " +
            "WHERE id = #{id} AND user_id = #{userId}")
    int update(Address address);

    @Delete("DELETE FROM address WHERE id = #{id} AND user_id = #{userId}")
    int delete(@Param("id") Long id, @Param("userId") Long userId);

    @Select("SELECT name FROM address WHERE user_id = #{userId}")
    String findAddressNameByUserId(Long userId);

    @Select("SELECT * FROM address WHERE id = #{id}")
    Address findById(Long id);
} 